﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>RoleModel</title>
    <link href="~/Content/layui/css/layui.css" rel="stylesheet" />
</head>
<body>
    <div id="test12" class="demo-tree-more"></div>
    <div class="layui-btn-container">
        <button type="button" class="layui-btn layui-btn-sm" id="btn1">删除角色模块</button>
        <button type="button" class="layui-btn layui-btn-sm" id="btn2">添加角色模块</button>
    </div>
    <script src="~/Content/layui/layui.js"></script>
    <script>
        layui.use(['tree', 'util'], function () {
            var tree = layui.tree
                , layer = layui.layer
                , util = layui.util
                , $ = layui.$
                //模拟数据2
            $.get("/Role/ModuleData", {}, function (res) {
                //基本演示
                tree.render({
                    elem: '#test12'
                    , data: res
                    , showCheckbox: true  //是否显示复选框
                    , id: 'demoId1'


                });
                tree.setChecked('demoId1', [@ViewBag.ModuleId]); //勾选指定节点
            });
            //按钮事件
            $("#btn1").click(function () {
                var a = tree.getChecked('demoId1');
                var ids1 = "";
                var ids2 = "";
                for (var i = 0; i < a.length; i++) {
                    ids1 += a[i].id + ",";
                    for (var j = 0; j < a[i].children.length; j++) {
                        ids2 += a[i].children[j].id + ",";
                    }
                }
                var ids3 = ids1 + ids2;
                ids3 = ids3.substring(0, ids3.length - 1);
                if (ids3 == "") {
                    layer.msg("没有要删除的数据");
                } else {
                    $.post("/Back/Role/SetModule", { ids: ids3, id:@ViewBag.RoleId }, function (res) {
                    if (res.Success) {
                        layer.msg(res.Message);
                    } else {
                        layer.msg(res.Message);
                    }
                })
                }

            });
             $("#btn2").click(function () {
                var a = tree.getChecked('demoId1');
                var ids1 = "";
                var ids2 = "";
                for (var i = 0; i < a.length; i++) {
                    ids1 += a[i].id + ",";
                    for (var j = 0; j < a[i].children.length; j++) {
                        ids2 += a[i].children[j].id + ",";
                    }
                }
                var ids3 = ids1 + ids2;
                 ids3 = ids3.substring(0, ids3.length - 1);
                 if (ids3 == "") {
                     layer.msg("请选择要添加的角色模块");
                 }  else {
                     $.post("/Back/Role/SetModuleAdd", { ids: ids3, id:@ViewBag.RoleId }, function (res) {
                         if (res.Success) {
                             layer.msg(res.Message);
                         } else {
                             layer.msg(res.Message);
                         }
                     });
                 }

            });
        });
    </script>
</body>
</html>
